import {type RouteRecordRaw, createRouter, createWebHashHistory} from 'vue-router';
const Layout = () => import('@/layout/index.vue');

export const constantRoutes: RouteRecordRaw[] = [
    {
        path: '/404',
        component: () => import('@/views/error-page/404.vue'),
        meta: {
            hidden: true
        },
        alias: '/:pathMatch(.*)*'
    },
    {
        path: '/unauthorized',
        component: () => import('@/views/error-page/unauthorized.vue'),
        meta: {
            hidden: true
        }
    },
    {
        path: '/login',
        component: () => import('@/views/home-page/index.vue'),
        meta: {
            hidden: true
        }
    },
    {
        path: '/',
        component: Layout,
        redirect: '/home',
        children: [
            {
                path: 'home',
                component: () => import('@/views/home-page/index.vue'),
                name: 'Dashboard',
                meta: {
                    title: '首页',
                    svgIcon: 'dashboard',
                    affix: true
                }
            },
            {
                path: '/hot-zone-recommendation',
                component: () =>
                    import(
                        /* webpackChunkName: "hot-zone-recommendation" */ '@/views/hot-zone-recommendation/index.vue'
                    ),
                name: 'HotZoneRecommendation',
                meta: {
                    title: '热区推荐管理',
                    svgIcon: 'hot'
                }
            },
            {
                path: '/recommended-web',
                component: () => import(/* webpackChunkName: "recommended-web" */ '@/views/recommended-web/index.vue'),
                name: 'RecommendedWeb',
                meta: {
                    title: '推荐网站管理',
                    svgIcon: 'menu'
                }
            },
            {
                path: '/script-instructions',
                component: () =>
                    import(/* webpackChunkName: "script-instructions" */ '@/views/script-instructions/index.vue'),
                name: 'ScriptInstructions',
                meta: {
                    title: '脚本指令管理',
                    svgIcon: 'script'
                }
            },
            {
                path: '/tool-management',
                component: () => import(/* webpackChunkName: "tool-management" */ '@/views/tool-management/index.vue'),
                name: 'ToolManagement',
                meta: {
                    title: '工具管理',
                    svgIcon: 'tool'
                }
            },
            {
                path: '/type-management',
                component: () => import(/* webpackChunkName: "type-management" */ '@/views/type-management/index.vue'),
                name: 'TypeManagement',
                meta: {
                    title: '类型管理',
                    svgIcon: 'type'
                }
            }
        ]
    }
];

/**
 * 动态路由
 */
export const asyncRoutes: RouteRecordRaw[] = [
    {
        path: '/:pathMatch(.*)*', // Must put the 'ErrorPage' route at the end, 必须将 'ErrorPage' 路由放在最后
        redirect: '/404',
        name: 'ErrorPage',
        meta: {
            hidden: true
        }
    }
];

const router = createRouter({
    history: createWebHashHistory(),
    routes: constantRoutes
});

/** 重置路由 */
export function resetRouter() {
    // 注意：所有动态路由路由必须带有 Name 属性，否则可能会不能完全重置干净
    try {
        router.getRoutes().forEach((route) => {
            const {name, meta} = route;
            if (name && meta.roles?.length) {
                router.hasRoute(name) && router.removeRoute(name);
            }
        });
    } catch {
        // 强制刷新浏览器也行，只是交互体验不是很好
        window.location.reload();
    }
}

export default router;
